package com.zhangyj.impl.actions;

import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.zhangyj.action.AbstractAction;
import com.zhangyj.action.RequestAction;
import com.zhangyj.action.RequestMethod;
import com.zhangyj.data.tables.BMenu;
import com.zhangyj.db.DBUtil;
import com.zhangyj.impl.UserContext;
import com.zhangyj.impl.ViewName;
import com.zhangyj.util.JodoTreeCreator;
import com.zhangyj.util.JodoTreeCreator.Item;
@RequestAction(name="welcome")
public class Welcome extends AbstractAction {
	private static final long serialVersionUID = 1L;

	@RequestMethod(name = "load")
	public String load() throws Exception {
		return ViewName.welcome;
	}

	@RequestMethod(name = "menu")
	public void menu() throws Exception {
		List<Item> list = getFunctionList();
		JodoTreeCreator jtc = new JodoTreeCreator(list);
		Writer writer = response.getWriter();
		writer.append(jtc.asJSON());
	}

	private List<Item> getFunctionList() throws Exception {
		List<Item> returnlist = new ArrayList<Item>();
		UserContext userContext = UserContext.getUserContext(request);

		int templateId = DBUtil.queryOne(" select  menutemplateid  from b_menutemplate  where deleted = 0 and  entid = ?", Integer.class, userContext.getEntid());
		List<BMenu> lm = DBUtil.queryList("select *  from b_menu   where  menutemplateid = ? order by pmenuid,sort", BMenu.class, templateId);

		if (lm == null) {
			return returnlist;
		}
		for (Iterator<BMenu> iter = lm.iterator(); iter.hasNext();) {
			BMenu m = (BMenu) iter.next();

			Item item = new Item(m.getMenuId(), m.getMenuName(), m.getPmenuId(), m.getProgramId());
			if (item.programid < 1) {
				returnlist.add(item);
				continue;
			}
			// if (set.contains(item.programid)) {
			returnlist.add(item);
			// }
		}
		return returnlist;
	}
}
